package cn.ming.wfchat.app.me.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.dialog.CurrencyListDialog;
import cn.ming.wfchat.app.dialog.PaymentDialog;
import cn.ming.wfchat.app.me.activity.model.TransferAccoountBean;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.ming.wfchat.kit.utils.Tools;
import cn.wildfirechat.remote.ChatManager;

/**
 * 转账
 */
public class TransferActivity extends WfcBaseActivity {


    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.et_amount)
    EditText etAmount;
    @BindView(R.id.tv_toAddress)
    TextView tvToAddress;
    @BindView(R.id.tv_fee)
    TextView tvFee;
    @BindView(R.id.tv_fj_amount)
    TextView tvFjAmount;
    @BindView(R.id.cb_check)
    CheckBox cbCheck;
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.ll_type)
    LinearLayout llType;
    @BindView(R.id.ll_adds)
    LinearLayout llAdds;
    private String pledgeAmount;
    private String fee;
    private String userID;
    private String unit = "ETH";
    private String coinEnum = "ETH_USDT";


    @Override
    protected void afterViews() {
        super.afterViews();
        initDate();
        setTitle("转账");

    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_transfer;
    }

    void initDate() {
        userID = getIntent().getStringExtra("userID");
        toTransferAccount(coinEnum);
        etAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String editable = etAmount.getText().toString();
                String regEx = "\\.\\d{6}";  //只能输入字母或数字
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(editable);
                String str = m.replaceFirst("").trim();    //删掉不是字母或数字的字符
                if (!editable.equals(str)) {
                    etAmount.setText(str);  //设置EditText的字符
                    etAmount.setSelection(str.length()); //因为删除了字符，要重写设置新的光标所在位
                    Tools.showToast(TransferActivity.this, "小数点后输入不能超过5位");
                }
//                float amoutf = 0;
//                float feef = 0;
//                try {
//                    amoutf = Float.valueOf(str);
//                    feef = Float.valueOf(fee);
//                } catch (NumberFormatException e) {
//                    e.printStackTrace();
//                }
//              tvDaozhang.setText((amoutf - amoutf * feef) + "");

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        llType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CurrencyListDialog.newInstance().setOnClickListener(new CurrencyListDialog.OnClickListener() {
                    @Override
                    public void goOk(String selected) {
                        coinEnum = selected;
                        tvType.setText(selected);
                        toTransferAccount(selected);
                    }
                }).show(TransferActivity.this);
            }
        });
        cbCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    tvFee.setText(pledgeAmount + "FJ");
                } else {
                    tvFee.setText(fee);
                }
            }
        });

    }

    @OnClick(R.id.tv_next)
    void toTransfer() {
        if (TextUtils.isEmpty(etAmount.getText().toString().trim())) {
            Tools.showToast(TransferActivity.this, "请输入金额");
            return;
        }
        PaymentDialog.newInstance("付款", etAmount.getText().toString().trim() + coinEnum).setOnClickListener(new PaymentDialog.OnClickListener() {
            @Override
            public void getPwd(String password) {
                AppService.Instance().transferAccount(etAmount.getText().toString().trim(), password, userID, coinEnum, cbCheck.isChecked(),
                        new AppService.SendCodeCallback() {
                            @Override
                            public void onUiSuccess() {
                                finish();
                            }

                            @Override
                            public void onUiFailure(String msg) {
                                ChatManager.showToast(msg);
                            }
                        });
            }
        }).show(TransferActivity.this);

    }

    void toTransferAccount(String type) {
        if (type.contains("ETH")) {
            unit = "ETH";
        } else if (type.contains("TRX")) {
            unit = "TRX";
        } else {
            unit = "FJ";
        }
        AppService.Instance().toTransferAccount(type, userID, new AppService.TransferAccoountCallback() {
            @Override
            public void onUiSuccess(TransferAccoountBean rechargeBean) {
                if (unit.contains("FJ")) {
                   llAdds.setVisibility(View.GONE);
                    tvAmount.setText(rechargeBean.getFjAmount().toPlainString() + unit);
                } else {
                    llAdds.setVisibility(View.VISIBLE);
                    tvToAddress.setText(rechargeBean.getToAddress());
                    tvFee.setText(rechargeBean.getFee().toPlainString() + unit);
                    tvFjAmount.setText("可用" + rechargeBean.getFjAmount().toPlainString() + "FJ");
                    tvAmount.setText(rechargeBean.getAmount().toPlainString() + unit);
                    pledgeAmount = rechargeBean.getPledgeAmount() + "";
                    fee = rechargeBean.getFee() + unit;
                }
            }

            @Override
            public void onUiFailure(int code, String msg) {
                ChatManager.showToast(msg);
            }
        });
    }

}
